/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_notice.service.impl;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.yuecai.modules.yc_notice.domain.YcNotice;
import com.yuecai.modules.yc_notice.repository.YcNoticeRepository;
import com.yuecai.modules.yc_notice.service.dto.YcNoticeDto;
import com.yuecai.modules.yc_notice.service.dto.YcNoticeQueryCriteria;
import com.yuecai.modules.yc_notice.service.mapstruct.YcNoticeMapper;
import com.yuecai.utils.FileUtil;
import com.yuecai.utils.PageUtil;
import com.yuecai.utils.QueryHelp;
import com.yuecai.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yc_notice
* @date 2020-08-24
**/
@Service
@RequiredArgsConstructor
public class YcNoticeService {

    private final YcNoticeRepository ycNoticeRepository;
    private final YcNoticeMapper ycNoticeMapper;

    
    public Map<String,Object> queryAll(YcNoticeQueryCriteria criteria, Pageable pageable){
        Page<YcNotice> page = ycNoticeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ycNoticeMapper::toDto));
    }

    
    public List<YcNoticeDto> queryAll(YcNoticeQueryCriteria criteria){
        return ycNoticeMapper.toDto(ycNoticeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    
    @Transactional
    public YcNoticeDto findById(Long id) {
        YcNotice ycNotice = ycNoticeRepository.findById(id).orElseGet(YcNotice::new);
        ValidationUtil.isNull(ycNotice.getId(),"YcNotice","id",id);
        return ycNoticeMapper.toDto(ycNotice);
    }

    
    @Transactional(rollbackFor = Exception.class)
    public YcNoticeDto create(YcNotice resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return ycNoticeMapper.toDto(ycNoticeRepository.save(resources));
    }

    
    @Transactional(rollbackFor = Exception.class)
    public void update(YcNotice resources) {
        YcNotice ycNotice = ycNoticeRepository.findById(resources.getId()).orElseGet(YcNotice::new);
        ValidationUtil.isNull( ycNotice.getId(),"YcNotice","id",resources.getId());
        ycNotice.copy(resources);
        ycNoticeRepository.save(ycNotice);
    }

    
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            ycNoticeRepository.deleteById(id);
        }
    }

    
    public void download(List<YcNoticeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YcNoticeDto ycNotice : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("消息标题", ycNotice.getTitle());
            map.put("图片", ycNotice.getImage());
            map.put("内容", ycNotice.getContent());
            map.put("是否删除", ycNotice.getDel());
            map.put("创建时间", ycNotice.getCreateTime());
            map.put("过期时间", ycNotice.getExpirationTime());
            map.put("0群发，1个人", ycNotice.getNoticeType());
            map.put("给个人发送时有值", ycNotice.getUserId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}